package com.open.common.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class IdUtils {

	/**
	 * 获取账号
	 * @return  String
	 */
	/**
	 * 获取10位唯一账号（雪花算法简化版）
	 * @return String 10位唯一账号
	 */
	public static String getAccount() {
		return SnowflakeIdGenerator.generateId();
	}

	/**
	 * 简化的雪花算法ID生成器
	 */
	private static class SnowflakeIdGenerator {
		// 起始时间戳 (2020-01-01)
		private static final long START_TIMESTAMP = 1577836800000L;

		// 各部分位数
		private static final long MACHINE_ID_BITS = 2L;  // 机器标识位数
		private static final long SEQUENCE_BITS = 6L;    // 序列号位数

		// 各部分最大值
		private static final long MAX_MACHINE_ID = (1L << MACHINE_ID_BITS) - 1;
		private static final long MAX_SEQUENCE = (1L << SEQUENCE_BITS) - 1;

		// 各部分向左的位移
		private static final long MACHINE_LEFT_SHIFT = SEQUENCE_BITS;
		private static final long TIMESTAMP_LEFT_SHIFT = SEQUENCE_BITS + MACHINE_ID_BITS;

		private static final long machineId;
		private static long sequence = 0L;
		private static long lastTimestamp = -1L;

		static {
			// 简单的机器ID生成
			machineId = Math.abs(new Random().nextInt()) % (MAX_MACHINE_ID + 1);
		}

		public static synchronized String generateId() {
			long timestamp = System.currentTimeMillis();

			if (timestamp < lastTimestamp) {
				throw new RuntimeException("时钟回退，拒绝生成ID");
			}

			if (timestamp == lastTimestamp) {
				sequence = (sequence + 1) & MAX_SEQUENCE;
				if (sequence == 0) {
					timestamp = waitNextMillis(lastTimestamp);
				}
			} else {
				sequence = 0L;
			}

			lastTimestamp = timestamp;

			// 生成ID
			long id = ((timestamp - START_TIMESTAMP) << TIMESTAMP_LEFT_SHIFT)
					| (machineId << MACHINE_LEFT_SHIFT)
					| sequence;

			// 转换为10位字符串
			return String.format("%010d", Math.abs(id) % 10000000000L);
		}

		private static long waitNextMillis(long lastTimestamp) {
			long timestamp = System.currentTimeMillis();
			while (timestamp <= lastTimestamp) {
				timestamp = System.currentTimeMillis();
			}
			return timestamp;
		}
	}

	/**
	 * 用前缀和时间戳生成订单号（订单号长前缀+12+6)
	 * @author waming
	 */
	public static String getOrderNo(final String prefix) {
		DateFormat df = new SimpleDateFormat("yyMMddHHmmss");
		String last = "";
		synchronized (df) {
			Date date = null;
			date = new Date();
			last=getUUIDLong().substring(13);
			return prefix + df.format(date)+last;
		}
	}

	/**
	 * 用前缀和时间戳生成订单号 时间格式自定义
	 */
	public static String getOrderNo(final String prefix, String format) {
		DateFormat df = new SimpleDateFormat(format);
		String last = "";
		synchronized (df) {
			Date date = null;
			date = new Date();
			last=getUUIDLong().substring(13);
			return prefix + df.format(date)+last;
		}
	}

	public static String getUUID() {
		UUID uuid = UUID.randomUUID();
		return uuid.toString();
	}

	/**
	 * 产生唯一随机的一个数
	 * @return String
	 */
	public static String getUUIDLong() {
		UUID uuid = UUID.randomUUID();
		String rand = Math.abs(uuid.getLeastSignificantBits()) + "";
		return rand;
	}
}